import vue from '@vitejs/plugin-vue'
import path from "path";
import AutoImport from "unplugin-auto-import/vite";
import {NaiveUiResolver} from "unplugin-vue-components/resolvers";
import Components from "unplugin-vue-components/vite";
import {defineConfig} from "vite";
import {prismjsPlugin} from "vite-plugin-prismjs";
import postCssPxToRem from "postcss-pxtorem"
import {createSvgIconsPlugin} from "vite-plugin-svg-icons";

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        AutoImport({
            imports: ["vue", "vue-router", "pinia"],
            dts: "src/types/auto-imports.d.ts",
        }),
        Components({
            resolvers: [NaiveUiResolver()],
            dts: "src/types/components.d.ts",
        }),
        createSvgIconsPlugin({
            // 指定需要缓存的图标文件夹
            iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
            // 指定symbolId格式
            symbolId: "icon-[dir]-[name]",
        }),
        prismjsPlugin({
            languages: [
                "java", "python", "html", "css", "sass", "less", "go",
                "cpp", "c", "js", "ts", "sql", "bash", "git", "nginx", "php",
            ],
            theme: "tomorrow",
            css: true,
        }),
    ],
    css:{
        // 此代码为适配移动端px2rem
        postcss: {
            plugins: [
                postCssPxToRem({
                    rootValue: 54, // 1rem的大小
                    propList: ['*'], // 需要转换的属性，这里选择全部都进行转换
                })
            ]
        }
    },
    resolve: {
        // https://cn.vitejs.dev/config/#resolve-alias
        alias: {
            // 设置路径
            "~": path.resolve(__dirname, "./"),
            // 设置别名
            "@": path.resolve(__dirname, "./src"),
        },
        // https://cn.vitejs.dev/config/#resolve-extensions
        extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
    },
    server: {
        hmr: {
            overlay: false
        },
        open: true,
        proxy: {
            "/api": {
                target: "http://localhost:8081",
                // target: "http://43.142.156.249:8091",
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ""),
            },
        },
    },
})
